<?php


namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_category\Entity\ShusyCategory;
use Drupal\shusy_store\Entity\ShusyStore;
use Drupal\shusy_store_category\Entity\ShusyStoreCategory;

/**
 * @OA\Schema(
 *   schema="MyTeamStore",
 *   type="object",
 *   description="MyTeamStore"
 * )
 */
class MyTeamStore extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_store';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyStore) {
      $value = [
        /**
         * @OA\Property(
         *   property="id",
         *   type="integer",
         *   description="ID",
         * ),
         */
        'id' => (int) $this->entity->id,
        /**
         * @OA\Property(
         *   property="name",
         *   type="string",
         *   description="店铺名",
         * ),
         */
        'name' => (string) $this->entity->name,
        /**
         * @OA\Property(
         *   property="category",
         *   ref="#/components/schemas/CategoryItem",
         *   description="店铺分类 1级分类",
         * ),
         */
        'category' => $this->getCategory($this->entity),
        /**
         * @OA\Property(
         *   property="logo",
         *   type="string",
         *   description="LOGO url",
         * ),
         */
        'logo' => $this->imageStyleUrl('StoreItemLogo', file_load($this->entity->logo)),
        /**
         * @OA\Property(
         *   property="open",
         *   type="string",
         *   description="营业时间",
         * ),
         */
        'open' => (string) $this->entity->open,
        /**
         * @OA\Property(
         *   property="tel",
         *   type="string",
         *   description="店铺电话",
         * ),
         */
        'tel' => (string) $this->entity->phone,
        /**
         * @OA\Property(
         *   property="address",
         *   type="string",
         *   description="店铺地址",
         * ),
         */
        'address' => (string) $this->entity->store_address,
        /**
         * @OA\Property(
         *   property="longitude",
         *   type="number",
         *   description="经度",
         * ),
         */
        'longitude' => (float) $this->entity->longitude,
        /**
         * @OA\Property(
         *   property="latitude",
         *   type="number",
         *   description="纬度",
         * ),
         */
        'latitude' => (float) $this->entity->latitude,
        /**
         * @OA\Property(
         *   property="status",
         *   type="integer",
         *   description="状态 0 审核中 1 已通过 2 未通过",
         * ),
         */
        'status' => (int) $this->entity->status,
      ];
      $this->merge($value);
    }
  }

  protected function getCategory(ShusyStore $shusyStore) {
    $es = shusy_store_category_load_multiple(FALSE, ['store_id' => $shusyStore->id]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyStoreCategory) {
        $category = shusy_category_load($e->category_id);
        if ($category instanceof ShusyCategory) {
          return CategoryItem::get(FALSE, FALSE, [
            $category->id,
            $category->name,
          ]);
        }
      }
    }
    return ['id' => 0, 'name' => ''];
  }


}
